let n = 5, m = 6, k = 2;
let arr1 = [1, 1, 1, 1, 2, 2, 3, 4]
let arr2 = [2, 3, 4, 5, 4, 5, 4, 5];
let arr3 = [1, 1, 3, 4, 1, 8, 2, 1];
let mapway = {}; mapcost = {}
for (let i = 0; i < arr1.length; i++) {
  if (mapway[arr1[i]]) {
    mapway[arr1[i]].push(arr2[i]);
    mapcost[arr1[i]].push(arr3[i]);
  } else {
    mapway[arr1[i]] = [arr2[i]];
    mapcost[arr1[i]] = [arr3[i]];
  }
}
let res = [], path = [1], cur = 0;
function dfs (start, step, maxval) {
  if (start == n && step <= k) {
    console.log(path, maxval)
    res.push(maxval);
    return;
  }
  if (step > k) {
    return;
  }
  mapway[start].forEach((item, index) => {
    if (path.indexOf(item) !== -1) return;
    path.push(item);
    cur = Math.max(mapcost[start][index], maxval)
    dfs(item, step + 1, cur)
    path.pop();
  })
}
dfs(1, 0, 0);